package com.cskaoyan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.cskaoyan.bean.bo.zy.ZYBrandCreateListBO;
import com.cskaoyan.bean.bo.zy.ZYCategoryCreateBO;
import com.cskaoyan.bean.common.BaseParam;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketBrand;
import com.cskaoyan.bean.marketBean.MarketBrandExample;
import com.cskaoyan.bean.marketBean.MarketCategory;
import com.cskaoyan.bean.vo.zy.*;
import com.cskaoyan.mapper.marketMapper.MarketBrandMapper;
import com.cskaoyan.mapper.marketMapper.MarketCategoryMapper;
import com.cskaoyan.mapper.marketMapper.MarketRegionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangYun
 * @since 2022/12/28 21:28
 */
@Service
public class MarketRegionServiceImpl implements MarketRegionService {
    @Autowired
    MarketRegionMapper marketRegionMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CommonData<ZYMarketRegionListVO> list(CommonData<ZYMarketRegionListVO> regionData) {
        List<ZYMarketRegionListVO> regionProvinceList = marketRegionMapper.selectProvinceByType();
        for (ZYMarketRegionListVO province : regionProvinceList) {
            List<ZYChildrenBeanX> municipalityList = marketRegionMapper.selectMunicipalityByPid(province.getId());
            for (ZYChildrenBeanX municipality : municipalityList) {
                List<ZYChildrenBean> districtList = marketRegionMapper.selectdistrictByPid(municipality.getId());
                municipality.setChildren(districtList);
            }
            province.setChildren(municipalityList);
        }
        PageInfo<ZYMarketRegionListVO> provinceList = new PageInfo<>(regionProvinceList);
        CommonData data = CommonData.data(provinceList);
        return data;
    }

    @Override
    public CommonData<MarketBrand> brandList(BaseParam param, Integer id, String name) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        if (!StringUtil.isEmpty(param.getOrder()) &&!StringUtil.isEmpty(param.getSort())){
            marketBrandExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        }
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        //marketBrandExample.setOrderByClause("id");
        List<MarketBrand> brandList = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(brandList);
        CommonData data = CommonData.data(marketBrandPageInfo);
        return data;
    }

    @Override
    public ZYBrandCreateListVO brandCreate(ZYBrandCreateListBO param) {
        MarketBrand marketBrand1 = new MarketBrand();

        Map<String, Object> map = BeanUtil.beanToMap(param, false, false);
        MarketBrand marketBrand = BeanUtil.fillBeanWithMap(map, marketBrand1, new CopyOptions(MarketBrand.class, true));
        /*BigDecimal floorPrice;
        if (param.getFloorPrice() != null) {
            floorPrice = new BigDecimal(param.getFloorPrice());
            marketBrand.setFloorPrice(floorPrice);
        }
        if (param.getDesc() != null) {
            marketBrand.setDesc(param.getDesc());
        }
        if (param.getName() != null) {
            marketBrand.setName(param.getName());
        }
        if (param.getPicUrl() != null) {
            marketBrand.setPicUrl(param.getPicUrl());
        }*/
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());

        marketBrandMapper.insertSelective(marketBrand);
        MarketBrand result = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        ZYBrandCreateListVO resultVO = new ZYBrandCreateListVO();

        // 用工具类来简化下面操作
        // 引入依赖<dependency>
        //             <groupId>cn.hutool</groupId>
        //             <artifactId>hutool-all</artifactId>
        //             <version>5.7.17</version>
        //         </dependency>
        //         <dependency>
        //             <groupId>org.projectlombok</groupId>
        //             <artifactId>lombok</artifactId>
        //             <optional>true</optional>
        //         </dependency>
        String[] ignoreProperties = {"deleted","sortOrder"};
        BeanUtil.copyProperties(result,resultVO,ignoreProperties);
        /*resultVO.setId(result.getId());
        resultVO.setDesc(result.getDesc());
        resultVO.setAddTime(result.getAddTime());
        resultVO.setUpdateTime(result.getUpdateTime());
        resultVO.setName(result.getName());
        resultVO.setFloorPrice(result.getFloorPrice());
        resultVO.setPicUrl(result.getPicUrl());*/

        return resultVO;
    }

    @Override
    public MarketBrand brandUpdate(MarketBrand param) {
        marketBrandMapper.updateByPrimaryKey(param);
        return param;
    }

    @Override
    public void brandDelete(MarketBrand param) {
        marketBrandMapper.deleteByPrimaryKey(param.getId());
    }

    @Override
    public CommonData<ZYMarketCategoryL1VO> categoryL1() {
        List<ZYMarketCategoryL1VO> categoryList = marketCategoryMapper.categoryL1();
        PageInfo<ZYMarketCategoryL1VO> pageInfo = new PageInfo<>(categoryList);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<ZYMarketCategoryListVO> categoryList() {
        List<ZYMarketCategoryListVO> resultList;
        resultList = marketCategoryMapper.categoryList();
        PageInfo<ZYMarketCategoryListVO> pageInfo = new PageInfo<>(resultList);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public ZYCategoryCreateVO categoryCreate(ZYCategoryCreateBO param) {
        MarketCategory target = new MarketCategory();
        String[] ignoreProperties = {"id","sortOrder","addTime","updateTime","deleted","children"};
        BeanUtil.copyProperties(param,target,ignoreProperties);
        Date date = new Date();
        target.setAddTime(date);
        target.setUpdateTime(date);
        marketCategoryMapper.insertSelective(target);
        MarketCategory result = marketCategoryMapper.selectByPrimaryKey(target.getId());
        ZYCategoryCreateVO resultVO = new ZYCategoryCreateVO();
        String[] ignoreAttribute = {"deleted","children","sortOrder"};
        BeanUtil.copyProperties(result,resultVO,ignoreAttribute);
        return resultVO;
    }

    @Override
    public void categoryUpdate(MarketCategory param) {
        marketCategoryMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void categoryDelete(MarketCategory param) {
        marketCategoryMapper.deleteByPrimaryKey(param.getId());
    }
}
